package test.enba.security.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enba.boot.core.base.Result;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import test.enba.security.auth.entity.User;
import test.enba.security.auth.service.IUserService;

/** 用户表 */
// @PreAuthorize("hasAuthority('order:select')")
@RestController
@RequestMapping("/auth/user")
public class UserController {

  @Autowired private IUserService userService;
  @Autowired private PasswordEncoder passwordEncoder;

  /**
   * 查询所有接口 （验收必须有指定角色）
   *
   * @return list
   */
  @PreAuthorize("hasRole('admin')")
  @GetMapping
  public Result<List<User>> findAll() {
    return Result.success(userService.list());
  }

  /**
   * 根据id查询数据接口 （验收必须有指定权限）
   *
   * @param id id
   * @return result
   */
  @PreAuthorize("hasAuthority('user:findOne')")
  @GetMapping("/{id}")
  public Result<User> findOne(@PathVariable Integer id) {
    return Result.success(userService.getById(id));
  }

  /**
   * 分页查询接口
   *
   * @param pageNum nume
   * @param pageSize size
   * @return result
   */
  @GetMapping("/page")
  public Result<Page<User>> findPage(
      @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("deleted", 0);
    return Result.success(userService.page(new Page<>(pageNum, pageSize), queryWrapper));
  }

  /**
   * 新增和更新接口
   *
   * @param user user
   * @return result
   */
  @PostMapping
  public Result<Boolean> save(@RequestBody User user) {
    return Result.success(userService.saveOrUpdate(user));
  }

  /**
   * 删除接口
   *
   * @param id id
   * @return result
   */
  @DeleteMapping("/{id}")
  public Result<Boolean> delete(@PathVariable Integer id) {
    return Result.success(userService.removeById(id));
  }

  /**
   * 批量删除接口
   *
   * @param ids ids
   * @return result
   */
  @PostMapping("/del/batch")
  public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
    return Result.success(userService.removeByIds(ids));
  }

  /**
   * 用户注册接口
   *
   * @param userName 用户
   * @param password 密码
   * @return result
   */
  @PostMapping("/register")
  public Result<Boolean> register(@RequestParam String userName, @RequestParam String password) {
    // 检查用户是否存在
    if (userService
        .getBaseMapper()
        .exists(Wrappers.<User>lambdaQuery().eq(User::getUsername, userName))) {
      return Result.err(500, "用户" + userName + "已存在");
    }

    User user = new User();
    user.setUsername(userName);
    user.setPassword(passwordEncoder.encode(password));

    // 新增并返回
    return Result.success(userService.save(user));
  }
}
